package com.riki.riki_push;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.huawei.agconnect.config.AGConnectServicesConfig;
import com.huawei.hmf.tasks.OnCompleteListener;
import com.huawei.hmf.tasks.Task;
import com.huawei.hms.aaid.HmsInstanceId;
import com.huawei.hms.common.ApiException;
import com.huawei.hms.push.HmsMessaging;
import com.riki.riki_push.util.CommonUtil;
import com.riki.riki_push.util.RomUtil;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

/**
 * RikiPushPlugin
 */
public class RikiPushHwPlugin implements FlutterPlugin, MethodCallHandler {
    /// The MethodChannel that will the communication between Flutter and native Android
    ///
    /// This local reference serves to register the plugin with the Flutter Engine and unregister it
    /// when the Flutter Engine is detached from the Activity
    private MethodChannel channel;
    private Context context;
    private HmsInstanceId hmsInstance;
    private HmsMessaging hmsMessaging;

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        context = flutterPluginBinding.getApplicationContext();
        hmsInstance = HmsInstanceId.getInstance(context);
        hmsMessaging = HmsMessaging.getInstance(context);
        channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "riki_push_hw");
        channel.setMethodCallHandler(this);
    }

    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
        if (call.method.equals("getToken")) {
            getHwToken(result);
        } else if (call.method.equals("deleteToken")) {
            deleteHwToken(result);
        } else {
            result.notImplemented();
        }
    }


    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
        channel.setMethodCallHandler(null);
    }

    // 获取厂商渠道
    private void getPhonePlatform(@NonNull Result result) {
        int phonePlatform = 0;
        if (RomUtil.isEmui()) {
            phonePlatform = 1;
        }
        if (RomUtil.isMiui()) {
            phonePlatform = 2;
        }
        if (RomUtil.isOppo()) {
            phonePlatform = 3;
        }
        if (RomUtil.isVivo()) {
            phonePlatform = 4;
        }
        if (RomUtil.isFlyme()) {
            phonePlatform = 5;
        }
        result.success(phonePlatform);
    }

    // 获取华为token
    private void getHwToken(@NonNull final Result result) {
        new Thread() {
            @Override
            public void run() {
                try {
                    String appId = AGConnectServicesConfig.fromContext(context).getString("client/app_id");
                    CommonUtil.runMainThreadReturn(result, hmsInstance.getToken(appId, HmsMessaging.DEFAULT_TOKEN_SCOPE));
                } catch (ApiException e) {
                    CommonUtil.runMainThreadReturnError(result, e.getStatusCode() + "", e.getMessage(), e.getMessage());
                }
            }
        }.start();
    }

    // 删除华为token
    private void deleteHwToken(@NonNull final Result result) {
        new Thread() {
            @Override
            public void run() {
                try {
                    String appId = AGConnectServicesConfig.fromContext(context).getString("client/app_id");
                    hmsInstance.deleteToken(appId, HmsMessaging.DEFAULT_TOKEN_SCOPE);
                    CommonUtil.runMainThreadReturn(result, null);
                } catch (ApiException e) {
                    CommonUtil.runMainThreadReturnError(result, e.getStatusCode() + "", e.getMessage(), e.getMessage());
                }
            }
        }.start();
    }


}
